package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.mapper.DesignFileMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class DesignFileServiceImpl extends ServiceImpl<DesignFileMapper, DesignFile>
    implements DesignFileService {

    @Autowired
    private DesignFileMapper designFileMapper;

    @Override
    public PageUtils<DesignFile> page(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<DesignFile>();
        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_type())) {
                wrapper.eq("type", vo.getSearch_type());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<DesignFile>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<DesignFile>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> unmakePage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加4个条件
        // 类型是商品
        wrapper.eq("type", "Y001-1");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");
        // 物料设计标志：未设计
        wrapper.eq("design_module_tag", "W001-0");

        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> unmakePage2(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加4个条件
        // 类型是商品
        wrapper.eq("type", "Y001-1");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");
        // 物料设计标志：未设计
        wrapper.eq("design_module_tag", "W001-1");
        // 工序组成设计标志：未设计
        wrapper.eq("design_procedure_tag", "G001-0");

        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> unmakePage3(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加4个条件
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");
        // 库存分配标志：未设计
        wrapper.eq("design_cell_tag", "K001-0");

        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public String getMaxFlowNumber() {
        DesignFile designFile=designFileMapper.queryMaxFile();
        //获取产品编号
        String productId=designFile.getProductId();
        //获取后6位流水号
        String flowNumber=productId.substring(9);
        return flowNumber;
    }

    /**
     * 按条件分页查询产品档案是物料的信息
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<DesignFile> moduleFilePage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加3个条件
        // 类型是物料
        wrapper.eq("type", "Y001-2");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");

        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> moduleFilePage2(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        // 额外添加3个条件
        // 类型是物料
        wrapper.eq("type", "Y001-1");
        // 审核状态：审核通过
        wrapper.eq("check_tag", "S001-1");
        // 删除状态：未删除
        wrapper.eq("delete_tag", "C001-0");

        if(vo != null) {
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(wrapper);
        PageInfo<DesignFile> pageInfo = new PageInfo<>(designFileList);

        // PageInfo 转换成 PageUtils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    /**
     * 按条件分页查询产品档案的未复核
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<DesignFile> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("thirdKindId",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_type())){
                queryWrapper.eq("type",vo.getSearch_type());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加3个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-0");
        // 删除状态：未删除
        queryWrapper.eq("delete_tag","C001-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<DesignFile> designFileList =this.list(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }
}
